package com.pearadmin.pro.common.tools.server.server;

import com.pearadmin.pro.common.tools.core.MathUtil;
import lombok.Setter;

import java.lang.management.ManagementFactory;

import static com.pearadmin.pro.common.tools.core.MathUtil.div;
import static com.pearadmin.pro.common.tools.core.MathUtil.mul;
import static java.lang.management.ManagementFactory.getRuntimeMXBean;

/**
 * JVM 信息
 *
 * Author: wangxiaozhe
 * CreateTime: 2021/04/01
 */
@SuppressWarnings("unused")
@Setter
public class Jvm {
    /**
     * 当前JVM占用的内存总数(M)
     */
    private double total;

    /**
     * JVM最大可用内存总数(M)
     */
    private double max;

    /**
     * JVM空闲内存(M)
     */
    private double free;

    /**
     * JDK版本
     */
    private String version;

    /**
     * JDK路径
     */
    private String home;

    public double getTotal() {
        return div(total, (1024 * 1024), 2);
    }

    public double getMax() {
        return div(max, (1024 * 1024), 2);
    }

    public double getFree() {
        return div(free, (1024 * 1024), 2);
    }

    public double getUsed() {
        return div(total - free, (1024 * 1024), 2);
    }

    public double getUsage() {
        return mul(div(total - free, total, 4), 100);
    }

    /**
     * 获取JDK名称
     */
    public String getName() {
        return getRuntimeMXBean().getVmName();
    }

    public String getVersion() {
        return version;
    }

    public String getHome() {
        return home;
    }

}
